package cn.ac.iscas.action.project;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.ac.iscas.bean.project.DocumentType;
import cn.ac.iscas.bean.project.Project;
import cn.ac.iscas.bean.project.ProjectDocument;
import cn.ac.iscas.service.project.IProjectDocumentService;
import cn.ac.iscas.service.project.IProjectService;

@Controller @Scope("prototype")
public class ProjectManageAction extends ActionSupport{
	/**
	 *处理项目的录入，保存，修改，删除的功能
	 */
	private static final long serialVersionUID = 2997402046169380022L;
	@Resource private IProjectService projectService;//运行时动态注入projectService层
	@Resource private IProjectDocumentService projectDocumentService;//运行时动态注入projectDocumentService层
	
	private Project project;//接收处理时动态注入
	private ProjectDocument document;//接收处理时动态注入
	
	/**
	 *上传时动态接收注入的文档文件
	 **/
	private File[] applyingDocument;
	private File[] ongoingDocument;
	private File[] finishedDocument;
	
	/**
	 *上传时动态接收注入的文档文件类型
	 **/
	private String[] applyingDocumentContentType;
	private String[] ongoingDocumentContentType;
	private String[] finishedDocumentContentType;
	
	/**
	 *上传时动态接收注入的文档文件名
	 **/
	private String[] applyingDocumentFileName;
	private String[] ongoingDocumentFileName;
	private String[] finishedDocumentFileName;
	/**
	 * 处理项目的详细查询
	 * */
	public String detailQuery()//详细信息查询
	{
		project = projectService.get(Project.class, project.getId());
	//	System.out.println(project.get);
		return "detailQuery";//跳转到详细信息界面
	}
	/**
	 * 处理项目的修改
	 * */
	public String edit()//项目信息修改
	{
		project = projectService.get(Project.class, project.getId());
		return "edit";//跳转修改界面
	}
	/**
	 * 处理项目的更新
	 * */
	public String update() throws IOException
	{
		//得到原有项目文档
		Set<ProjectDocument> documents = projectService.get(Project.class, project.getId()).getProjectDocuments();
		for(ProjectDocument doc:documents)//更新原有的项目文档
		{
			doc.setProject(project);
		}
		String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		
		Set<ProjectDocument> projectOngoingDocuments = new HashSet<ProjectDocument>();
		Set<ProjectDocument> projectApplyingDocuments = new HashSet<ProjectDocument>();
		Set<ProjectDocument> projectFinishedDocuments = new HashSet<ProjectDocument>();
		
		//处理已结题的文档更新
		if(finishedDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<finishedDocument.length;i++)
			{
				ProjectDocument docInfo = new ProjectDocument();
				String realName = UUID.randomUUID().toString()+ getExt(finishedDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(finishedDocument[i], savefile);
				docInfo.setDocName(finishedDocumentFileName[i]);
				docInfo.setContentType(finishedDocumentContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType(DocumentType.验收文档);
				docInfo.setSize(finishedDocument[i].length()/1024);
				docInfo.setProject(project);	
				projectFinishedDocuments.add(docInfo);
			}
		}
		
		//处理正在进行的文档更新
		if(ongoingDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<ongoingDocument.length;i++)
			{
				ProjectDocument docInfo = new ProjectDocument();
				String realName = UUID.randomUUID().toString()+ getExt(ongoingDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(ongoingDocument[i], savefile);
				docInfo.setDocName(ongoingDocumentFileName[i]);
				docInfo.setContentType(ongoingDocumentFileName[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType(DocumentType.执行中文档);
				docInfo.setSize(ongoingDocument[i].length()/1024);
				docInfo.setProject(project);	
				projectOngoingDocuments.add(docInfo);
			}
			System.out.println("上传执行中文档成功");
			System.out.println(realpath);
		}
		
		//处理申请的文档更新
		if(applyingDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<applyingDocument.length;i++)
			{
				ProjectDocument docInfo = new ProjectDocument();
				String realName = UUID.randomUUID().toString()+ getExt(applyingDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(applyingDocument[i], savefile);
				docInfo.setDocName(applyingDocumentFileName[i]);
				docInfo.setContentType(applyingDocumentContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType(DocumentType.申请立项文档);
				docInfo.setSize(applyingDocument[i].length()/1024);
				docInfo.setProject(project);//建立项目和文档的一对多关系
				projectApplyingDocuments.add(docInfo);
			}
		}
		
		//将项目更新导数据库中
		projectService.update(project);
		for(ProjectDocument doc:documents)
		{
			projectDocumentService.save(doc);//保存原有的项目文档
		}
		
		//保存项目文档  注意必须先保存文档再保存项目
		for(ProjectDocument doc:projectApplyingDocuments)
		{
			projectDocumentService.save(doc);
		}
		for(ProjectDocument doc:projectOngoingDocuments)
		{
			projectDocumentService.save(doc);
		}
		for(ProjectDocument doc:projectFinishedDocuments)
		{
			projectDocumentService.save(doc);
		}
		return "success";
	}
	/**
	 * 
	 * 项目保存
	 * */
	public String save() throws IOException
	{
		String realpath = ServletActionContext.getServletContext().getRealPath("/documents");
		Set<ProjectDocument> projectOngoingDocuments = new HashSet<ProjectDocument>();
		Set<ProjectDocument> projectApplyingDocuments = new HashSet<ProjectDocument>();
		Set<ProjectDocument> projectFinishedDocuments = new HashSet<ProjectDocument>();
		if(finishedDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<finishedDocument.length;i++)
			{
				ProjectDocument docInfo = new ProjectDocument();
				String realName = UUID.randomUUID().toString()+ getExt(finishedDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(finishedDocument[i], savefile);
				docInfo.setDocName(finishedDocumentFileName[i]);
				docInfo.setContentType(finishedDocumentContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType(DocumentType.验收文档);
				docInfo.setSize(finishedDocument[i].length()/1024);
				docInfo.setProject(project);	
				projectFinishedDocuments.add(docInfo);
			}
			System.out.println("上传验收文档中文档成功");
			System.out.println(realpath);
		}
		if(ongoingDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<ongoingDocument.length;i++)
			{
				ProjectDocument docInfo = new ProjectDocument();
				String realName = UUID.randomUUID().toString()+ getExt(ongoingDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(ongoingDocument[i], savefile);
				docInfo.setDocName(ongoingDocumentFileName[i]);
				docInfo.setContentType(ongoingDocumentFileName[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType(DocumentType.执行中文档);
				docInfo.setSize(ongoingDocument[i].length()/1024);
				docInfo.setProject(project);	
				projectOngoingDocuments.add(docInfo);
			}
			System.out.println("上传执行中文档成功");
			System.out.println(realpath);
		}
		if(applyingDocument != null)
		{
			File savedir = new File(realpath);
			if(!savedir.exists()) savedir.mkdir();
			for(int i=0; i<applyingDocument.length;i++)
			{
				ProjectDocument docInfo = new ProjectDocument();
				String realName = UUID.randomUUID().toString()+ getExt(applyingDocumentFileName[i]);
				File savefile = new File(savedir , realName);
				FileUtils.copyFile(applyingDocument[i], savefile);
				docInfo.setDocName(applyingDocumentFileName[i]);
				docInfo.setContentType(applyingDocumentContentType[i]);
				docInfo.setUploadRealName(realName);
				docInfo.setDocPath(realpath);
				docInfo.setUploadTime(new Date());
				docInfo.setDocumentType(DocumentType.申请立项文档);
				docInfo.setSize(applyingDocument[i].length()/1024);
				docInfo.setProject(project);	
				projectApplyingDocuments.add(docInfo);
			}
			System.out.println("上传申请立项文档成功");
			System.out.println(realpath);
		}
		projectService.save(project);
		for(ProjectDocument doc:projectApplyingDocuments)
		{
			projectDocumentService.save(doc);
			System.out.println("保存applyingdoc成功");
		}
		for(ProjectDocument doc:projectOngoingDocuments)
		{
			projectDocumentService.save(doc);
			System.out.println("保存onggoingdoc成功");
		}
		for(ProjectDocument doc:projectFinishedDocuments)
		{
			projectDocumentService.save(doc);
			System.out.println("保存projectFinishedDocuments成功");
		}
		return "success";
	}
	/**
	 * 
	 *删除项目文档Ajax
	 * */
	public String deleteDocument() throws IOException
	{
		document = projectDocumentService.get(ProjectDocument.class, document.getId());
		projectDocumentService.delete(document);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	/**
	 * 
	 *删除项目Ajax
	 * */
	public String delete() throws IOException
	{
		project = projectService.get(Project.class,project.getId());
		projectService.delete(project);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	public String deletePaperProjectBind() throws IOException
	{
		project = projectService.get(Project.class,project.getId());
		projectService.delete(project);
		ActionContext ctx = ActionContext.getContext();     
	    HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
	    response.setContentType("text/html;charset=utf-8");
	    PrintWriter out=response.getWriter();
	    response.setContentType("text/html;charset=utf-8");
	    out.println("成功删除");
		return null;
	}
	public String[] getApplyingDocumentContentType() {
		return applyingDocumentContentType;
	}
	public void setApplyingDocumentContentType(String[] applyingDocumentContentType) {
		this.applyingDocumentContentType = applyingDocumentContentType;
	}
	public String[] getOngoingDocumentContentType() {
		return ongoingDocumentContentType;
	}
	public void setOngoingDocumentContentType(String[] ongoingDocumentContentType) {
		this.ongoingDocumentContentType = ongoingDocumentContentType;
	}
	public String[] getFinishedDocumentContentType() {
		return finishedDocumentContentType;
	}
	public void setFinishedDocumentContentType(String[] finishedDocumentContentType) {
		this.finishedDocumentContentType = finishedDocumentContentType;
	}
	public static String getExt(String fileName)
	{
		return fileName.substring(fileName.lastIndexOf("."));
	}
	public void setDocument(ProjectDocument document) {
		this.document = document;
	}
	public ProjectDocument getDocument() {
		return document;
	}
	public File[] getFinishedDocument() {
		return finishedDocument;
	}
	public void setFinishedDocument(File[] finishedDocument) {
		this.finishedDocument = finishedDocument;
	}
	public String[] getFinishedDocumentFileName() {
		return finishedDocumentFileName;
	}
	public void setFinishedDocumentFileName(String[] finishedDocumentFileName) {
		this.finishedDocumentFileName = finishedDocumentFileName;
	}
	public File[] getOngoingDocument() {
		return ongoingDocument;
	}
	public void setOngoingDocument(File[] ongoingDocument) {
		this.ongoingDocument = ongoingDocument;
	}
	public String[] getOngoingDocumentFileName() {
		return ongoingDocumentFileName;
	}
	public void setOngoingDocumentFileName(String[] ongoingDocumentFileName) {
		this.ongoingDocumentFileName = ongoingDocumentFileName;
	}
	public IProjectDocumentService getProjectDocumentService() {
		return projectDocumentService;
	}
	public void setProjectDocumentService(
			IProjectDocumentService projectDocumentService) {
		this.projectDocumentService = projectDocumentService;
	}
	public File[] getApplyingDocument() {
		return applyingDocument;
	}
	public void setApplyingDocument(File[] applyingDocument) {
		this.applyingDocument = applyingDocument;
	}
	public String[] getApplyingDocumentFileName() {
		return applyingDocumentFileName;
	}
	public void setApplyingDocumentFileName(String[] applyingDocumentFileName) {
		this.applyingDocumentFileName = applyingDocumentFileName;
	}
	public IProjectService getProjectService() {
		return projectService;
	}
	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	
}
